package com.qingge.springboot.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingge.springboot.entity.User;
import com.qingge.springboot.service.RedisService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qingge.springboot.common.Result;

import com.qingge.springboot.service.IUserLikeService;
import com.qingge.springboot.entity.UserLike;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户点赞表 前端控制器
 * </p>
 *
 * @author web_work
 * @since 2023-02-18
 */
@RestController
@RequestMapping("/user-like")
public class UserLikeController {
    
    @Resource
    private IUserLikeService userLikeService;

    @Autowired
    private RedisService redisService;


    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody UserLike userLike) {
        userLikeService.saveOrUpdate(userLike);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        userLikeService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
    userLikeService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    public Result findAll() {
        return Result.success(userLikeService.list());
        }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(userLikeService.getById(id));
        }

    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
    @RequestParam Integer pageSize) {
        QueryWrapper<UserLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(userLikeService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }


    @GetMapping("/like")
    public Result like(@RequestParam("liked_message_id")Integer liked_message_id,
                       @RequestParam("liked_post_id") Integer liked_post_id){
        //先将数据存放到redis中
        redisService.saveLiked2Redis(liked_message_id,liked_post_id);
        redisService.incrementLikedCount(liked_message_id);
        return Result.success("点赞成功");
    }


    @GetMapping("/unlike")
    public Result unlike(@RequestParam("liked_message_id")Integer liked_message_id,
                       @RequestParam("liked_post_id") Integer liked_post_id){
        redisService.unlikeFromRedis(liked_message_id, liked_post_id);
        redisService.decrementLikedCount(liked_message_id);
        return Result.success("取消点赞");
    }

    @GetMapping("/islike")
    public Result IsLike(@RequestParam("liked_message_id") Integer liked_message_id,
                         @RequestParam("liked_post_id") Integer liked_post_id){
        if(redisService.GetLikeFromRedis(liked_message_id,liked_post_id)){
            return Result.success("已点赞");
        }else{
            QueryWrapper<UserLike> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("liked_message_id",liked_message_id);
            queryWrapper.eq("liked_post_id",liked_post_id);
            UserLike userLike= userLikeService.getOne(queryWrapper);
            if(userLike!=null&&userLike.getStatus())
                return Result.success("已点赞");
        }
        return Result.success("未点赞");
    }


//    @GetMapping("/test")
//    public Result test()
//    {
//        userLikeService.transLikedFromRedis2DB();
//        userLikeService.transLikedCountFromRedis2DB();
//        return Result.success();
//    }



}

